package com.zejson.controller;

import com.zejson.entity.Role;
import com.zejson.entity.User;
import com.zejson.service.RoleService;
import com.zejson.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

@Controller
@Scope("prototype")
@RequestMapping("/register")
public class RegisterController {

    @RequestMapping("")
    public ModelAndView index() throws ClassNotFoundException {
        ModelAndView model;
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        RoleService roleService = ((RoleService)applicationContext.getBean("roleService"));
        List roles = roleService.getList();
        roles = (List<Role>) roles;
        model = new ModelAndView("/templates/register/register");
        model.addObject("roles",roles);
        return model;
    }

    @RequestMapping("/register.do")
    public Object register(HttpServletRequest request) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        ModelAndView model;
        if(request.getMethod().equals("POST")){
            String username = request.getParameter("username");
            String sex = request.getParameter("sex");
            String email = request.getParameter("e-mail");
            String phone = request.getParameter("phone");
            String password1 = request.getParameter("password1");
            String password2 = request.getParameter("password2");

            if (!password1.equals(password2)){
                model = new ModelAndView("/templates/register/register");
                model.addObject("message", "The two passwords are inconsistent");
                return model;
            }

            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            UserService userService = ((UserService) applicationContext.getBean("userService"));
            List user = userService.getByProperty("Email", email);
            if(user.size()!=0){
                model = new ModelAndView("/templates/register/register");
                model.addObject("message", "this email already exists");
                return model;
            }

            User add_user = new User();
            add_user.setUsername(username);
            add_user.setEmail(email);
            add_user.setSex((byte) Integer.parseInt(sex));
            if(phone!="")
                add_user.setPhone(phone);
            add_user.setPassword(password1);

            add_user.setAccount("123321");
            add_user.setFkRoleid(4);
            add_user.setCheckintime(new Date());
            add_user.setIsdeleted((byte) 0);
            add_user.setUserid(10000);

            System.out.println(add_user);

            ApplicationContext applicationContext1 = new ClassPathXmlApplicationContext("applicationContext.xml");
            UserService userService1 = ((UserService) applicationContext1.getBean("userService"));
            userService1.insert("UserMapper", add_user);

            return "/templates/login/login";
        }

        model = new ModelAndView("/templates/register/register");
        return model;
    }
}
